<template>
    <div class="layout">
        <div class="box">
            <!--<p class="p">申请提交</p>-->
            <div class="head-line">申请提交</div>
            <div class="department">
                <span>申请部门</span>
                <el-select v-model="deptType" placeholder="相应部门名称" class="select">
                    <el-option
                        v-for="item in deptList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <ul class="ul-box">
                <li v-for="(item,index) in listApp" :key="item.id" v-if="index <5 || (index >=5&&show1)">
                    <el-checkbox v-model="item.checked" class="checkbox"></el-checkbox>
                    <span><b>{{item.name}}</b></span>
                    <div class="ip-a">
                        <span>已选{{item.choiceIpList.length}}个ip地址</span>
                        <el-link type="primary" @click="openIp(item)" class="button">查看
                        </el-link>
                    </div>
                    <div class="ip-b">
                        <p v-for="(items,index) in item.choiceIpList" :key="items.id"
                           v-if="(item.choiceIpList.length > 3 && index < 2) || item.choiceIpList.length < 4">
                            IP地址：{{items.ip}}</p>
                        <p v-if="item.choiceIpList.length > 3">...</p>
                    </div>
                </li>
                <li class="li-box" @click="visible1=true">
                    <i class="iconfont icon-plus"></i>
                    <p>
                        新增一个自建系统
                    </p>
                </li>
                <!--<button @click="show1=!show1" v-if="!show1">点击显示全部</button>-->
                <!--<button @click="show1=!show1" v-else>点击收起全部</button>-->
            </ul>
            <div class="text-center" v-if="listApp[0]&&listApp.length>5">
                <el-link type="primary" :underline="false" v-if="!show1" @click="showChange">点击显示全部</el-link>
                <el-link type="primary" :underline="false" v-else @click="showChange">点击收起全部</el-link>
            </div>

            <dialog-msk :show.sync="visible1" :width="'1000px'" :title="'新增自建系统'" @cancel="close">
                <div slot="footer" class="text-center">
                    <span slot="footer">
                        <el-button @click="close">取消</el-button>
                        <el-button type="primary" @click="save">确定</el-button>
                    </span>
                </div>
                <self-add :inside="true" ref="selfAdd"></self-add>
            </dialog-msk>
            <look :show.sync="visible" @save="ipSave" :ipLists.sync="appIpList"
                  :currentIp="currentIp"></look>
            <div class="message">
                <p>信息确认</p>
                <table-example :data="tableData" :tableColumn="header" :slotColumns="slotColumns" class="mt20">
                    <!--接受scope-->
                    <template slot="name" slot-scope="scope">
                        <!--                        <el-image :src="scope.row.icon" class="logo"></el-image>-->
                        <el-image
                            class="logo"
                            :src="scope.row.iconInfo&&scope.row.iconInfo.path"
                            fit="scale-down">
                            <div slot="placeholder" class="image-slot">
                                <i class="el-icon-loading"></i>
                            </div>
                            <div slot="error" class="image-slot">
                                <i class="el-icon-picture-outline"></i>
                            </div>
                        </el-image>
                        <span>{{scope.row.name}}</span>
                        <!--<span>{{scope.row.date}}</span>-->
                    </template>
                </table-example>
                <p>联系人</p>
                <el-input v-model="contact" class="input"></el-input>
                <p>联系电话</p>
                <el-input v-model="phone" class="input"></el-input>
                <p>申请说明</p>
                <el-input v-model="remark" type="textarea" :autosize="{ minRows: 4, maxRows: 4}"></el-input>
                <p>相关文件提交</p>
                <upload :fileList.sync="fileList" :multiple="true"></upload>

                <div class="text-center mt30">
                    <el-button type="primary" @click="submit">提交申请</el-button>
                </div>
            </div>
        </div>

    </div>
</template>

<script>
    import tableExample from '../../components/table'
    import upload from '../../components/upload'
    import look from '../../components/look'
    import dialogMsk from '../../components/dialog'
    import page from '../../components/pagination'
    import selfAdd from '../manager/self-built-add'

    export default {
        data() {
            return {
                tableData: [],
                tableData1: [],
                header: [

                    {
                        prop: 'name',
                        label: '名称',
                        minWidth: '50%',
                        align: 'center',
                        headerAlign: 'center'
                    },
                    {
                        prop: 'resSource',
                        label: '提供单位',
                        minWidth: '50%',
                        align: 'center',
                        headerAlign: 'center'
                    }
                ],
                deptList: [
                    {
                        value: 1,
                        label: '用户主要所属部门'
                    }
                    // {
                    //     value: 2,
                    //     label: '用户兼职部门'
                    // }
                ],
                slotColumns: ['name'],
                slotColumns1: ['ip', 'service', 'operate'],
                deptType: 1,
                fileList: [],
                file: [],
                contact: '', // 联系人
                phone: '',
                remark: '',
                checked: false,
                visible: false,
                visible1: false,
                name: '',
                show: false,
                data: {},
                listApp: [],
                appList: [],
                itemList: [],
                show1: false,
                flag: false,
                appIpList: [],
                currentIp: null,
                loading: false
            }
        },
        mounted() {
            this.getData()
        },
        update() {
            this.$method.updateView(true)
        },
        methods: {
            managerApps(service) {
                this.show = true
            },
            change() {
                if (this.checked) {
                    this.$refs.table.handleSelectionAll()
                } else {
                    this.$refs.table.handleSelectionClear()
                }
            },
            //  待确认订单明细
            getData() {
                this.loading = true
                this.$http.get('/gsOrder/toConfirmOrder', {
                    resId: this.$route.query.resId || ''
                }).then(result => {
                    this.loading = true
                    if (result.success) {
                        this.getList()
                        for (var i = 0; i < result.data.length; i++) {
                            this.tableData.push(result.data[i].res)
                            this.itemList.push({id: result.data[i].id})
                        }
                        this.$method.updateView()
                    }
                }).catch(() => {
                    this.loading = false
                })
            },
            //  自建系统列表
            getList() {
                this.loading = true
                this.$http.get('/dept/workbench/listApp').then(result => {
                    this.loading = false
                    if (result.success) {
                        for (let j = 0; j < result.data.length; j++) {
                            result.data[j].choiceIpList = result.data[j].appIpList
                            result.data[j].choiceIpList.forEach(item => {
                                item.checked = true
                            })
                        }
                        this.listApp = result.data
                    }
                }).catch(() => {
                    this.loading = false
                })
            },
            submit() {
                let ipIdList = []
                this.appList = []
                for (let i = 0; i < this.fileList.length; i++) {
                    this.file.push(this.fileList[i].id)
                }
                for (let j = 0; j < this.listApp.length; j++) {
                    if (this.listApp[j].checked) {
                        this.appList.push({
                            appCode: this.listApp[j].code,
                            ip: this.listApp[j].choiceIpList.map(item => item.ip).join(',')
                        })
                        this.listApp[j].choiceIpList.map(item => {
                            ipIdList.push(item.id)
                        })
                    }

                }
                if (!this.appList[0]) {
                    this.$method.messageBox('alert', '请选择自建系统')
                    return false
                }
                this.$http.post('/gsOrder/confirm', {
                    contactName: this.contact,
                    contactTel: this.phone,
                    remark: this.remark,
                    deptType: this.deptType,
                    enclosure: this.file.join(','),
                    appList: this.appList,
                    itemList: this.itemList,
                    ipIdList: ipIdList.join(',')
                }).then(result => {
                    if (result.success) {
                        this.$router.push({
                            path: '/spm/success',
                            query: {
                                orderNo: result.data
                            }
                        })
                    }
                })

            },
            openIp(item) {
                this.visible = true
                this.currentIp = item
                let list1 = item.appIpList, list2 = item.choiceIpList
                list1.forEach(item => {
                    let obj = list2.find((list) => {
                        return list.id === item.id
                    })
                    item.checked = obj && obj.ip
                })
                this.appIpList = list1
            },
            ipSave(val) {
                this.currentIp.choiceIpList = val
                this.visible = false
            },
            close() {
                this.$refs.selfAdd.ruleForm = {
                    name: '',
                    remark: ''
                }
                this.$refs.selfAdd.ipList = []
                this.$refs.selfAdd.loading = false
                this.visible1 = false
            },
            save() {
                let _this = this
                this.$refs.selfAdd.add(() => {
                    this.getList()
                    _this.close()
                })
            },
            showChange() {
                this.show1 = !this.show1
                this.$method.updateView(true)
            }
        },
        components: {
            tableExample,
            upload,
            look,
            dialogMsk,
            page,
            selfAdd
        }
    }
</script>

<style scoped lang="less">
    .box {
        margin: 0.4rem 0;

        .department {
            padding: 0.36rem 0.3rem;
            margin: 0.3rem 0;
            background-color: #fff;

            .select {
                width: 4.6rem;
                height: 0.5rem;
            }
        }

        ul {
            width: 12.5rem;
            /*height: 5.7rem;*/
            overflow: hidden;

            li {
                float: left;
                width: 3.8rem;
                height: 2.55rem;
                margin-right: 0.3rem;
                margin-bottom: 0.3rem;
                padding: 0.22rem 0.3rem;
                padding-bottom: 0;
                background-color: #fff;

                .checkbox {
                    width: 0.16rem;
                    height: 0.16rem;
                }

                span {
                    font-size: 0.2rem;
                    color: #333;
                    padding-left: 0.05rem;
                }

                .ip-a {
                    margin-top: 0.1rem;
                    border-bottom: 0.01rem solid #EFEFEF;
                    padding-bottom: 0.2rem;

                    .button {
                        border: none;
                        background-color: #fff;
                        color: #4995F4;
                        margin-left: 0.3rem;
                    }

                    span {
                        font-size: 0.16rem;
                    }
                }

                .ip-b {
                    padding-top: 0.15rem;

                    p {
                        font-size: 0.14rem;
                        color: #999;
                        padding: 0.04rem;
                    }

                }
            }

            .li-box {
                text-align: center;
                color: #dcdcdc;
                font-size: .16rem;
                padding-top: .57rem;

                i {
                    font-size: .65rem;
                }
            }

            .built-info {
                .line {
                    margin-bottom: .2rem;

                    > * {
                        display: inline-block;
                        max-width: 9rem;
                    }

                    label {
                        float: left;
                        width: 1.5rem;
                        text-align: right;
                        margin-right: 0.15rem;
                        line-height: .4rem;
                        font-size: .16rem;
                    }

                    .el-link {
                        line-height: .4rem;
                    }
                }
            }

            .built-address {
                margin-top: .3rem;
            }
        }

        .message {
            margin-top: 0.3rem;
            /*height: 14.16rem;*/
            background-color: #fff;
            padding: 0.5rem 0.4rem;
            padding-top: 0;

            p {
                padding-top: 0.5rem;
                padding-bottom: 0.2rem;
            }

            .input {
                width: 4.6rem;
            }

            .logo {
                margin-left: 1.5rem;
                margin-right: -1rem;
                width: .5rem;
                height: .22rem;
                float: left;
                /*background-color: pink;*/
            }
        }

        .btn {
            width: 2rem;
            height: 0.5rem;
            margin-top: 1rem;
            margin-left: 4.7rem;
        }

    }

</style>
